package com.sqx.modules.taking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.entity.UserVip;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.app.service.UserVipService;
import com.sqx.modules.taking.dao.GameDao;
import com.sqx.modules.taking.dao.OrderTakingDao;
import com.sqx.modules.taking.entity.Game;
import com.sqx.modules.taking.entity.OrderTaking;
import com.sqx.modules.taking.service.GameService;
import com.sqx.modules.taking.service.OrderTakingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class GameServiceImpl extends ServiceImpl<GameDao, Game> implements GameService {

    @Autowired
    private OrderTakingService orderTakingService;

    @Autowired
    private OrderTakingDao orderTakingDao;
    @Autowired
    private UserService userService;
    @Autowired
    private UserVipService vipService;

    @Override
    public Result queryGameName(Long parentId) {
        QueryWrapper<Game> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("status", 0)
                .eq("parent_id", parentId)
                .orderByAsc("sort");
        List<Game> games = baseMapper.selectList(queryWrapper);
        if (parentId == 0) {
            for (Game game : games) {
                game.setParentList(baseMapper.selectList(new QueryWrapper<Game>().eq("parent_id", game.getId()).eq("status", 0).orderByAsc("sort")));
                game.setChildren(game.getParentList());
            }
        }
        return Result.success().put("data", games);
    }

    @Override
    public Result selectClassifyServiceList(String city, String longitude, String latitude) {
        Page<OrderTaking> pages = new Page<>(1, 3);
        List<Game> selectList = baseMapper.selectList(new QueryWrapper<Game>().eq("parent_id", 0).eq("status", 0).orderByAsc("sort"));
        ArrayList<Long> arrayList = new ArrayList<>();
        for (Game game : selectList) {
            List<Game> children = baseMapper.selectList(new QueryWrapper<Game>().eq("parent_id", game.getId()).eq("status", 0).orderByAsc("sort"));
            for (Game child : children) {
                arrayList.add(child.getId());
            }
            if (arrayList.size() > 0) {
                game.setOrderTakingList(orderTakingDao.getOrderTakingListByGameId(pages, arrayList, longitude, latitude, 3, null, null, city, null).getRecords());
                arrayList.clear();
            }
        }
        return Result.success().put("data", selectList);
    }

    @Override
    public Result addGameName(Game game) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        game.setCreateTime(simpleDateFormat.format(new Date()));
        game.setUpdateTime(simpleDateFormat.format(new Date()));
        game.setStatus((long) 0);
        int i = baseMapper.insert(game);
        return Result.success();
    }

    @Override
    public Result updateGameName(Game game) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        game.setUpdateTime(simpleDateFormat.format(new Date()));
        int i = baseMapper.updateById(game);
        return Result.success();
    }

    @Override
    public Result deleteGameName(Long id) {

        Game game = baseMapper.selectById(id);
        if (game != null) {
            baseMapper.deleteById(id);
            return Result.success();
        } else {
            return Result.error("游戏信息不存在！");

        }
    }

    @Override
    public Result queryAllGameName(Long page, Long limit, Long parentId) {
        if (page == null || limit == null) {
            List<Game> gameList = baseMapper.selectList(new QueryWrapper<Game>().eq("parent_id", parentId).orderByAsc("sort"));
            return Result.success().put("data", gameList);
        } else {
            Page<Game> page1 = new Page<>(page, limit);
            IPage<Game> gameIPage = baseMapper.selectPage(page1, new QueryWrapper<Game>().eq("parent_id", parentId).orderByAsc("sort"));
            List<Game> games = gameIPage.getRecords();
            if (parentId == 0) {
                for (Game game : games) {
                    game.setParentList(baseMapper.selectList(new QueryWrapper<Game>().eq("parent_id", game.getId())));
                    game.setChildren(game.getParentList());
                }
            }
            return Result.success().put("data", gameIPage);
        }

    }

    @Override
    public Result enableGameName(Long status, Long id) {
        Game game = baseMapper.selectById(id);
        if (game == null) {
            return Result.error("游戏分类信息不存在");
        } else {
            game.setStatus(status);
            game.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            baseMapper.updateById(game);
            return Result.success();
        }

    }

    @Override
    public List<Game> getGameList(Game game) {
        return baseMapper.selectList(new QueryWrapper<>(game));
    }

    @Override
    public Result selectClassifyList(Integer status) {
        List<Game> selectList = baseMapper.selectList(new QueryWrapper<Game>().in("parent_id", 0).eq("status", status).orderByAsc("sort"));
        for (Game children : selectList) {
            List<Game> gameList = baseMapper.selectList(new QueryWrapper<Game>().eq("parent_id", children.getId()).eq("status", status).orderByDesc("sort"));
            children.setChildren(gameList);
        }
        return Result.success().put("data", selectList);
    }

    @Override
    public IPage<OrderTaking> selectServiceList(Integer page, Integer limit, Long id, String longitude, String latitude, Integer condition, String salesNum, String by, String city, Long userId) {
        Page<OrderTaking> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        Game game = baseMapper.selectById(id);
        if (game == null) {
            return null;
        }
        Integer isVip = null;
        UserEntity userEntity = userService.getById(userId);
        if (userEntity != null) {
            UserVip userVip = vipService.getOne(new QueryWrapper<UserVip>().eq("user_id", userId));
            if (userVip != null && userVip.getIsVip() != null && userVip.getIsVip() == 1) {
                isVip = 1;
            } else {
                isVip = 2;
            }
        }
        ArrayList<Long> arrayList = new ArrayList<>();
        if (game.getParentId() == 0) {
            List<Game> gameList = baseMapper.selectList(new QueryWrapper<Game>().eq("parent_id", game.getId()).eq("status", 0).orderByDesc("sort"));
            for (Game children : gameList) {
                arrayList.add(children.getId());
            }
        } else {
            arrayList.add(game.getId());
        }
        return orderTakingDao.getOrderTakingListByGameId(pages, arrayList, longitude, latitude, condition, salesNum, by, city, isVip);

    }

    /**
     * 查询二级分类排序
     */
    @Override
    public Result queryEjGameSort() {
        List<Game> list = orderTakingDao.queryEjGameSort();
        return Result.success().put("data", list);
    }

    @Override
    public IPage<OrderTaking> selectRmServiceList(Integer page, Integer limit, String longitude, String latitude, Integer condition, String salesNum, String by, String city, Long userId) {
        Page<OrderTaking> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        Integer isVip = null;
        UserEntity userEntity = userService.getById(userId);
        if (userEntity != null) {
            UserVip userVip = vipService.getOne(new QueryWrapper<UserVip>().eq("user_id", userId));
            if (userVip != null && userVip.getIsVip() != null && userVip.getIsVip() == 1) {
                isVip = 1;
            } else {
                isVip = 2;
            }
        }
        ArrayList<Long> arrayList = new ArrayList<>();
        List<Game> gameList = baseMapper.selectList(new QueryWrapper<Game>().eq("status", 0).orderByDesc("sort"));
        for (Game children : gameList) {
            arrayList.add(children.getId());
        }
        return orderTakingDao.getOrderTakingListByGameId(pages, arrayList, longitude, latitude, condition, salesNum, by, city, isVip);
    }


}
